package com.yyys.web.controller;

import com.yyys.web.entity.DRInfo;
import com.yyys.web.json.RestaurantInfoJson;
import com.yyys.web.service.DRInfoService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhengsenwen on 2017/7/8.
 */

@RestController
@RequestMapping("/restaurant")
public class RestaurantController {

    private static Logger logger = Logger.getLogger(RestaurantController.class);

    @Autowired
    private DRInfoService drInfoService;

    /**
     * 首页餐厅实时信息  main.html/ajax
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/drInfoList", method = RequestMethod.GET)
    public List<DRInfo> main(Model model) {
        List<DRInfo> drInfoList = new ArrayList<>();

        try {
            drInfoList = drInfoService.getAll();
        } catch (Exception e) {
            logger.info("main:" + e.getMessage());
            e.printStackTrace();
        }
        return drInfoList;
    }


    /**
     * 获取餐厅的所有信息和各餐厅对应的推荐就餐时间  main.html/ajax
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/restaurantInfoList", method = RequestMethod.GET)
    public List<RestaurantInfoJson> getRestaurantInfo(Model model) {
        List<RestaurantInfoJson> restaurantInfoJsonList = new ArrayList<>();
        List<DRInfo> drInfoList = new ArrayList<>();

        //// TODO: 2017/9/8 获取餐厅信息和最佳就餐时间

        try {
            drInfoList = drInfoService.getAll();
        } catch (Exception e) {
            logger.info("getRestaurantInfo:" + e.getMessage());
            e.printStackTrace();
        }

        for (DRInfo drInfo : drInfoList) {
            RestaurantInfoJson restaurantInfoJson = new RestaurantInfoJson();
            restaurantInfoJson.setRecommendedMealTime(drInfoService.getRecommendedMealTime(drInfo.getId()));
            restaurantInfoJson.setDrInfo(drInfo);

            restaurantInfoJsonList.add(restaurantInfoJson);
        }

        logger.info("restaurantInfoJsonList:" + restaurantInfoJsonList);

        return restaurantInfoJsonList;

    }

    /**
     * 该餐厅实时进出情况 最佳就餐时间  diningroom.html/ajax
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/drInfo/{drId}", method = RequestMethod.GET)
    public RestaurantInfoJson diningroom(Model model, @PathVariable("drId") Integer drId) {
        DRInfo drInfo = new DRInfo();
        try {
            drInfo = drInfoService.getDrInfoById(drId);
        } catch (Exception e) {
            logger.info("diningroom:" + e.getMessage());
            e.printStackTrace();
        }

        RestaurantInfoJson restaurantInfoJson = new RestaurantInfoJson();
        restaurantInfoJson.setRecommendedMealTime(drInfoService.getRecommendedMealTime(drId));
        restaurantInfoJson.setDrInfo(drInfo);

        return restaurantInfoJson;
    }


    /**
     * 获取推荐最佳就餐时间 ajax
     *
     * @param model
     * @param drId
     * @return
     */
    @RequestMapping(value = "/best/meal/time/{drId}", method = RequestMethod.GET)
    public String getBestMealTime(Model model, @PathVariable Integer drId) {
        return drInfoService.getRecommendedMealTime(drId);
    }

}
